package com.jt.utils;

import org.springframework.stereotype.Component;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class FileUtil {
    /*文件上传和读取*/
    public void fileUpLoud(String path, File file) {
        OutputStream out = null;
        InputStream in = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String formatString = format.format(new Date());
            String pathBenDi = path + formatString;
            File fileBenDi = new File(pathBenDi);
            if (!fileBenDi.isDirectory()) {
                fileBenDi.mkdirs();
            }
            File fileResult = new File(fileBenDi + file.getName());
            if (!fileResult.exists()) {
                fileBenDi.createNewFile();
            }
            byte[] bytes = new byte[1024];
            int lenth = 0;
            out = new FileOutputStream(fileBenDi);
            in = new FileInputStream(file);
            while ((lenth = in.read()) != -1) {
                out.write(bytes, 0, lenth);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
